<?php
if(! defined('BASEPATH')) {
	exit('No direct script access allowed');
}
/**
 *站内信
 *@author 王春林
 */
class stationLetters extends MY_Controller {
	function __construct() {
		parent::__construct();
		$this->load->helper('url');
		$this->load->model("stationLetter_model");
		$this->load->model("friendsreferral_model");
	}
	function index() {
		$this->listReceivedLetters();
	}
	/**
	 *查询所有收到的信件
	 */
	function listReceivedLetters() {
		$loginId = $this->session->userdata('userId');
		$data = array();
		$data['results'] = $this->stationLetter_model->listReceivedLetters($loginId);
		$data['userList'] = $this->listFriendsByLoginId($loginId);
		log_message('info',json_encode($data['results']));
		$this->getHeader();
		$this->load->view("stationReceivedLetters",$data);
	}
	
	
	/**
	 *查询所有发出的信件
	 */
	function listSendLetters() {
		$loginId = $this->session->userdata("userId");
		$data = array();
		$data['results'] = $this->stationLetter_model->listSendLetters($loginId);
		$data['userList'] = $this->listFriendsByLoginId($loginId);
		log_message("info",json_encode($data['userList']));
		$this->getHeader();
		$this->load->view("stationSendLetters",$data);
	}
	/**
	 *查询收到的提示
	 */
	function listUnreadLetters() {
		$loginId = $this->session->userdata("userId");
		$isRead = "未读";
		$data = array();
		$data['results'] = $this->stationLetter_model->listLettersByReadStatus($loginId,$isRead);
		$data['userList'] = $this->listFriendsByLoginId($loginId);
		$this->getHeader();
		$this->load->view("stationReceivedTips",$data);
	}
	/**
	 *写信
	 */
	function writeLetter() {
		$loginId = $this->session->userdata("userId");
		$data = $_POST;
		$data['createTime'] = date("Y-m-d H:i:s");
		$data['sender'] = $loginId;
		$data['isRead'] = "未读";
		$this->stationLetter_model->writeLetter($data);
		$this->listReceivedLetters();
	}
	/**
	 *根据ID删除收到的信件
	 */
	function deleteReceivedLetterById() {
		$letterId = $_POST['letterId'];
		$this->stationLetter_model->deleteReceivedLetterById($letterId);
	}
	/**
	 *根据ID删除发出的信件
	 */	
	function deleteSendLetterById() {
		log_message('info',json_encode($_POST));
		$letterId = $_POST['letterId'];
		$this->stationLetter_model->deleteSendLetterById($letterId);
	}
	/**
	 *根据ID查询收到信件的详细信息
	 */	
	function queryReceivedLetterContentById() {
		$letterId = $_GET['letterId'];
		$data = array();
		$data['results'] = $this->stationLetter_model->queryReceivedLetterContentById($letterId);
		log_message('info','log_data'.json_encode($data['results']));
		$this->getHeader();
		$this->load->view("stationReceivedLetterContent",$data);
	}
	/**
	 *根据ID查询收到信件的详细信息
	 */	
	function echoJsonReceivedLetterContentById() {
		$letterId = $_GET['letterId'];
		$data = array();
		$data['results'] = $this->stationLetter_model->queryReceivedLetterContentById($letterId);
		log_message('info','log_data'.json_encode($data['results']));
		echo json_encode($data['results']);
	}
	/**
	 *根据ID查询发出信件的详细信息
	 */	
	function querySendLetterContentById() {
		$letterId = $_GET['letterId'];
		$data = array();
		$data['results'] = $this->stationLetter_model->querySendLetterContentById($letterId);
		$this->getHeader();
		$this->load->view("stationSendLetterContent",$data);
	}
	/**
	 *回复信件
	 */	
	function replyLetter() {
		$data = $_POST;
		$flag = $data['sender'];
		$data['sender'] = $data['recipient'];
		$data['recipient'] = $flag;
		$this->stationLetter_model->replyLetter($data);
		$this->listSendLetters();
	}
	/**
	 *获取所有好友列表
	 */
	 function listFriendsByLoginId($loginId) {
		$data['userId'] = $loginId;
		$data['uncheckTeamId'] = -2;
		$datas = $this->friendsreferral_model->queryFriendsList($data);
		log_message('info',json_encode($datas));
		return $datas;
	 }
}
?>